let config = require('./config.js')
let domain = config.API
/**
 * 设置authorization
 */
function setauthorization(key, value) {
	return new Promise(function(resolve, reject) {
		uni.setStorage({
			key: key,
			data: value,
			success: function() {
				resolve();
			},
			fail: function() {
				uni.showToast({
					title: "设置缓存失败",
					icon: "none"
				})
			}
		});
	})
}

/**
 * 获取authorization
 */
function getauthorization(key) {
	try {
		const value = uni.getStorageSync(key);
		if (value) {
			return value;
		}
	} catch (e) {
		// error
	}
	return "";
}

/**
 * 发送请求
 */
function request(obj, that) {
	let url = obj.url ? obj.url : "";
	let data = obj.data ? obj.data : {};
	let method = obj.method ? obj.method : "POST";
	let fail = obj.fail ? obj.fail : null;


	// 处理authorization
	let authorization = ''
	if (that.authorization) {
		authorization = that.authorization;
	} else {
		authorization = getauthorization("authorization");
		if (authorization) {
			that.authorization = authorization;
		}
	}


	return new Promise(function(resolve, reject) {

		let isShow = false; // 是否显示出加载动画
		let timer = setTimeout(function() {
			uni.showLoading({
				title: "加载中"
			})
			isShow = true;
		}, 500)

		if (url.indexOf("wx/auth/getByPhone") != -1) {
			console.info("--------->登录")
			uni.request({
				url: domain + url,
				method: method,
				data: data,
				success(res) {
					clearTimeout(timer);
					if (isShow) {
						uni.hideLoading();
					}

					// 认证token不合法
					if (res.data.code === 604 || res.data.code === 601) {
						// 登录失效重新登录
						uni.clearStorageSync("authorization");
						that.authorization = null;
						uni.navigateTo({
							url: "/pages/login/login"
						})
						return;
					}

					if (res.data.code != 0) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						});
						setTimeout(function() {
							typeof fail === "function" ? fail(res) : '';
							reject(res)
						}, 1000)
						return;
					}
					resolve(res);
				},
				fail: () => {
					uni.showToast({
						title: "请求失败",
						icon: "none"
					});
				}
			})
		} else {
			console.info("--------->非登录:" + authorization)

			uni.request({
				url: domain + url,
				method: method,
				header: {
					"authorization": authorization
				},
				data: data,
				success(res) {
					clearTimeout(timer);
					if (isShow) {
						uni.hideLoading();
					}

					// 认证token不合法
					if (res.data.code === 604 || res.data.code === 601) {
						// 登录失效重新登录
						uni.clearStorageSync("authorization");
						that.authorization = null;
						uni.navigateTo({
							url: "/pages/login/login"
						})
						return;
					}

					if (res.data.code != 0) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						});
						setTimeout(function() {
							typeof fail === "function" ? fail(res) : '';
							reject(res)
						}, 1000)
						return;
					}
					resolve(res);
				},
				fail: () => {
					uni.showToast({
						title: "请求失败",
						icon: "none"
					});
				}
			})
		}


	})
}

export default {
	setauthorization: setauthorization,
	getauthorization: getauthorization,
	request: request,
	domain: domain
}
